
package com.bwt.usermanage.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import com.bwt.usermanage.R;

import java.util.Calendar;

public class DialogUtil {
	// 显示数据列表的对话框，自定义选中后逻辑处理
    public static void showListDialog(Context context, String[] items, String title, DialogInterface.OnClickListener listener){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title)
                .setItems(items, listener)
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();
    }

    // 显示提示信息，仅有关闭对话框按钮（文字可自定义）
    public static void showIntroDialog(Context context,String title,String message,String btnText){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title)
                .setMessage(message)
                .setNegativeButton(btnText,mListener)
                .show();
    }

    // 显示自定义view的对话框
    public static Dialog showViewDialog(Context context, String title, View view, DialogInterface.OnClickListener listener){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        return builder.setTitle(title)
                .setView(view)
                .setNegativeButton(R.string.cancel,listener)
                .setPositiveButton(R.string.confirm,listener)
                .show();
    }

    public static void showDatePicker(Context context,TextView textView) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog datePickerDialog = new DatePickerDialog( context, DatePickerDialog.THEME_HOLO_LIGHT,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        // 处理选择的日期
                        textView.setText(DateUtil.handleDate(year, month, dayOfMonth));
                    }
                }, year, month, day);
        datePickerDialog.show();
    }

    // 显示信息的对话框，按钮逻辑自定义
    public static Dialog showMsgDialog(Context context, String title, String message, DialogInterface.OnClickListener negativeListener,DialogInterface.OnClickListener positiveListener){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        return builder.setTitle(title)
                .setMessage(message)
                .setNegativeButton(R.string.cancel,negativeListener)
                .setPositiveButton(R.string.confirm,positiveListener)
                .show();
    }
    
    // 预设监听，仅含关闭对话框
    private static DialogInterface.OnClickListener mListener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which){
                case DialogInterface.BUTTON_NEGATIVE:
                    dialog.dismiss();
            }
        }
    };
}

